﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.InteropServices;

namespace SRB.Tg.Operate
{
    public class IniOper
    {
        #region 配置参数
        private const string USER_CONFIG_FILE_NAME = "system.ini";
        private const string USER_CONFIG_SECYION = "SYSTEM";

        // 系统参数
        private const string USER_CONFIG_KEY_TK = "CurTKPath";
        private const string USER_CONFIG_KEY_Sheet = "CurMapSheet";
        private const string USER_CONFIG_KEY_CurFont = "CurFont";
        private const string USER_CONFIG_KEY_Font = "Font";
        private const string USER_CONFIG_KEY_Font_Width = "FontWidth";
        private const string USER_CONFIG_KEY_BH = "BottomHeight";
        private const string USER_CONFIG_KEY_LW = "LeftWidth";
        private const string USER_CONFIG_KEY_TH = "TopHeight";
        private const string USER_CONFIG_KEY_RW = "RightWidth";
        private const string USER_CONFIG_KEY_ScaleList = "ScaleList";
        private const string USER_CONFIG_KEY_MU = "ModelUnit";
        private const string USER_CONFIG_KEY_LU= "LayoutUnit";

        #endregion

        #region 参数
        // 参数
        public string CurTKPath;
        public string CurMapSheet;
        public string CurFont;
        public string Font;
        public string FontWidth;
        public string BottomHeight;
        public string LeftWidth;
        public string TopHeight;
        public string RightWidth ;
        public string ScaleList;
        public string ModelUnit;
        public string LayoutUnit;
        #endregion

        #region 创建单例对象
        private IniOper() { }
        public static IniOper instance = null;
        public static object singletonLock = new object();

        public static IniOper getInstance()
        {
            if(instance == null)
            {
                lock(singletonLock)
                {
                    if (instance == null)
                    {
                        instance = new IniOper();
                    }
                }
            }

            return instance;
        }
        #endregion

        #region 设置/获取参数


        #endregion

        public void SaveConifg()
        {
            IniFileHelper iniFileHelper = new IniFileHelper();

            // 登录参数
            iniFileHelper.WriteIniString(USER_CONFIG_SECYION, USER_CONFIG_KEY_TK, CurTKPath);
            iniFileHelper.WriteIniString(USER_CONFIG_SECYION, USER_CONFIG_KEY_Sheet, CurMapSheet);
            iniFileHelper.WriteIniString(USER_CONFIG_SECYION, USER_CONFIG_KEY_CurFont, CurFont);
            iniFileHelper.WriteIniString(USER_CONFIG_SECYION, USER_CONFIG_KEY_Font, Font);
            iniFileHelper.WriteIniString(USER_CONFIG_SECYION, USER_CONFIG_KEY_Font_Width, FontWidth);
            iniFileHelper.WriteIniString(USER_CONFIG_SECYION, USER_CONFIG_KEY_BH, BottomHeight);
            iniFileHelper.WriteIniString(USER_CONFIG_SECYION, USER_CONFIG_KEY_LW, LeftWidth);
            iniFileHelper.WriteIniString(USER_CONFIG_SECYION, USER_CONFIG_KEY_TH, TopHeight);
            iniFileHelper.WriteIniString(USER_CONFIG_SECYION, USER_CONFIG_KEY_RW, RightWidth);
            iniFileHelper.WriteIniString(USER_CONFIG_SECYION, USER_CONFIG_KEY_ScaleList, ScaleList);
            iniFileHelper.WriteIniString(USER_CONFIG_SECYION, USER_CONFIG_KEY_MU, ModelUnit);
            iniFileHelper.WriteIniString(USER_CONFIG_SECYION, USER_CONFIG_KEY_LU, LayoutUnit);

        }

        public void LoadConifg()
        {
            IniFileHelper iniFileHelper = new IniFileHelper();
            StringBuilder sb = new StringBuilder(300);

            iniFileHelper.GetIniString(USER_CONFIG_SECYION, USER_CONFIG_KEY_TK, "", sb, sb.Capacity);
            CurTKPath = sb.ToString();
            if(!File.Exists(CurTKPath))
            {
                CurTKPath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "Template\\TK_A3.dwg";
            }

            iniFileHelper.GetIniString(USER_CONFIG_SECYION, USER_CONFIG_KEY_Sheet, "", sb, sb.Capacity);
            CurMapSheet = sb.ToString();
            iniFileHelper.GetIniString(USER_CONFIG_SECYION, USER_CONFIG_KEY_CurFont, "", sb, sb.Capacity);
            CurFont = sb.ToString();
            iniFileHelper.GetIniString(USER_CONFIG_SECYION, USER_CONFIG_KEY_Font, "", sb, sb.Capacity);
            Font = sb.ToString();
            iniFileHelper.GetIniString(USER_CONFIG_SECYION, USER_CONFIG_KEY_Font_Width, "", sb, sb.Capacity);
            FontWidth = sb.ToString();
            iniFileHelper.GetIniString(USER_CONFIG_SECYION, USER_CONFIG_KEY_BH, "", sb, sb.Capacity);
            BottomHeight = sb.ToString();
            iniFileHelper.GetIniString(USER_CONFIG_SECYION, USER_CONFIG_KEY_LW, "", sb, sb.Capacity);
            LeftWidth = sb.ToString();           
            iniFileHelper.GetIniString(USER_CONFIG_SECYION, USER_CONFIG_KEY_TH, "", sb, sb.Capacity);
            TopHeight = sb.ToString();
            iniFileHelper.GetIniString(USER_CONFIG_SECYION, USER_CONFIG_KEY_RW, "", sb, sb.Capacity);
            RightWidth = sb.ToString();
            iniFileHelper.GetIniString(USER_CONFIG_SECYION, USER_CONFIG_KEY_ScaleList, "", sb, sb.Capacity);
            ScaleList = sb.ToString();
            iniFileHelper.GetIniString(USER_CONFIG_SECYION, USER_CONFIG_KEY_MU, "", sb, sb.Capacity);
            ModelUnit = sb.ToString();
            iniFileHelper.GetIniString(USER_CONFIG_SECYION, USER_CONFIG_KEY_LU, "", sb, sb.Capacity);
            LayoutUnit = sb.ToString();
            
        }


    }

    class IniFileHelper
    {
        string strIniFilePath;  // ini配置文件路径
        public IniFileHelper()
        {
            strIniFilePath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "Config\\system.ini";
        }

        // 返回0表示失败，非0为成功
        [DllImport("kernel32", CharSet = CharSet.Auto)]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);

        // 返回取得字符串缓冲区的长度
        [DllImport("kernel32", CharSet = CharSet.Auto)]
        private static extern long GetPrivateProfileString(string section, string key, string strDefault, StringBuilder retVal, int size, string filePath);

        [DllImport("Kernel32.dll", CharSet = CharSet.Auto)]
        public static extern int GetPrivateProfileInt(string section, string key, int nDefault, string filePath);


        /// <summary>
        /// 获取ini配置文件中的字符串
        /// </summary>
        /// <param name="section">节名</param>
        /// <param name="key">键名</param>
        /// <param name="strDefault">默认值</param>
        /// <param name="retVal">结果缓冲区</param>
        /// <param name="size">结果缓冲区大小</param>
        /// <returns>成功true,失败false</returns>
        public bool GetIniString(string section, string key, string strDefault, StringBuilder retVal, int size)
        {
            long liRet = GetPrivateProfileString(section, key, strDefault, retVal, size, strIniFilePath);
            return (liRet >= 1);
        }


        /// <summary>
        /// 获取ini配置文件中的整型值
        /// </summary>
        /// <param name="section">节名</param>
        /// <param name="key">键名</param>
        /// <param name="nDefault">默认值</param>
        /// <returns></returns>
        public int GetIniInt(string section, string key, int nDefault)
        {
            return GetPrivateProfileInt(section, key, nDefault, strIniFilePath);
        }


        /// <summary>
        /// 往ini配置文件写入字符串
        /// </summary>
        /// <param name="section">节名</param>
        /// <param name="key">键名</param>
        /// <param name="val">要写入的字符串</param>
        /// <returns>成功true,失败false</returns>
        public bool WriteIniString(string section, string key, string val)
        {
            long liRet = WritePrivateProfileString(section, key, val, strIniFilePath);
            return (liRet != 0);
        }


        /// <summary>
        /// 往ini配置文件写入整型数据
        /// </summary>
        /// <param name="section">节名</param>
        /// <param name="key">键名</param>
        /// <param name="val">要写入的数据</param>
        /// <returns>成功true,失败false</returns>
        public bool WriteIniInt(string section, string key, int val)
        {
            return WriteIniString(section, key, val.ToString());
        }
    }
}
